let useraddMoudle = (() => {
  let
    { userId } = window.location.href.getUrlEncodedParams(),
    userdepartment = $('.userdepartment'),
    userjob = $('.userjob'),
    username = $('.username'),
    spanusername = $('.spanusername'),
    useremail = $('.useremail'),
    spanuseremail = $('.spanuseremail'),
    userphone = $('.userphone'),
    spanuserphone = $('.spanuserphone'),
    userdesc = $('.userdesc'),
    submit = $('.submit');

  // 姓名校验声明
  let checkName = function () {
    let name_value = username.val().trim();
    // 姓名必填校验
    if (name_value.length == 0) {
      spanusername.html("姓名为必填项")
      return false
    }
    // 姓名中文格式校验
    if (!/^[\u4e00-\u9fa5]{2,10}(·[\u4e00-\u9fa5]{2,10})*$/.test(name_value)) {
      spanusername.html("姓名必须为2-10个汉字，可带间名")
      return false
    }
    spanusername.html('')
    return true
  }
  // email校验声明
  let checkEmail = function () {
    let name_value = useremail.val().trim();
    // 姓名必填校验
    if (name_value.length == 0) {
      spanuseremail.html("邮箱为必填项")
      return false
    }
    // 邮箱格式校验
    if (!/^\w+(\.\w+|-\w+)*@[a-zA-Z0-9]+([.\-][a-zA-Z0-9]+)*(\.[a-zA-Z0-9]+)$/.test(name_value)) {
      spanuseremail.html("请验证邮箱格式，填写真实邮箱")
      return false
    }
    spanuseremail.html('')
    return true
  }
  // 电话校验声明
  let checkPhone = function () {
    let name_value = userphone.val().trim();
    // 姓名必填校验
    if (name_value.length == 0) {
      spanuserphone.html("电话为必填项")
      return false
    }
    // 姓名中文格式校验
    if (!/^1\d{10}$/.test(name_value)) {
      spanuserphone.html("请填写正确的电话号")
      return false
    }
    spanuserphone.html('')
    return true
  }

  // 获取下拉框数据，并渲染进下拉选项
  let get_options = async function () {
    // 做localStorage缓存，在退出时清除缓存
    let departmentData = await queryDepartment();
    let jobData = await queryJob();
    // 渲染下拉框
    if (!departmentData.code) {
      departmentData = departmentData.data;
      let str = ``;
      departmentData.forEach(item => {
        str += `<option value="${item.id}">${item.name}</option>`
      })
      userdepartment.html(str)
    }
    if (!jobData.code) {
      jobData = jobData.data;
      let str = ``;
      jobData.forEach(item => {
        str += `<option value="${item.id}">${item.name}</option>`
      })
      userjob.html(str)
    }
  }
  // 第一次校验
  let firstCheck = () => {
    username.blur(checkName);
    useremail.blur(checkEmail);
    userphone.blur(checkPhone);
  }
  // 提交事件绑定
  let secondCheck = () => {
    let handleSubmit = async function () {
      if (!(checkName() || checkEmail() || checkPhone())) {
        return false
      }
      // 整合表单信息
      let params = {
        name: username.val().trim(),
        sex: $('#man').prop('checked') ? 0 : 1,
        email: useremail.val().trim(),
        phone: userphone.val().trim(),
        departmentId: userdepartment.val().trim(),
        jobId: userjob.val().trim(),
        desc: userdesc.val().trim()
      }
      if (userId) {
        params.userId = userId;
        let res = await axios.post('/user/update', params);
        if (!res.code) {
          alert('修改员工成功')
          window.location.href = 'userlist.html'
          return
        }
        alert('修改失败')
      } else {
        let res = await axios.post('/user/add', params);
        if (!res.code) {
          alert('添加员工成功')
          window.location.href = 'userlist.html'
          return
        }
        alert('添加失败')
      }
    }
    submit.click(handleSubmit)
  }
  // 判断
  let judge = async function () {
    await get_options();
    // 如果userId存在，则是修改操作，需要装填
    if (userId) {
      let res = await axios.get('/user/info', { params: { userId } })
      if (!res.code) {
        let { id, name, sex, email, phone, departmentId, department, jobId, job, desc } = res.data;
        username.val(name);
        sex == 0 ? $('#man').prop('checked', true) : $('#woman').prop('checked', true);
        useremail.val(email);
        userphone.val(phone);

        userdepartment.val(departmentId);
        userjob.val(jobId);

        userdesc.val(desc);
      }
    }
  }

  return {
    init() {
      // 判断是否是修改，若是则默认装填信息
      judge();
      // 第一次失去焦点校验
      firstCheck();
      // 第二次提交校验
      secondCheck();
    }
  }
})()

useraddMoudle.init()